﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Serialization;
using FinMkts.CFD.DataService;
using FinMkts.CFD.Rest;
using Microsoft.Practices.Composite.Events;

namespace FinMkts.Client
{
    public partial class MainPage : UserControl
    {
        private IEventAggregator _eventAggregator;
        private IInstrumentService _instrumentService;

        [ImportingConstructor]
        public MainPage(IEventAggregator eventAggregator, IInstrumentService instrumentService)
        {
            _eventAggregator = eventAggregator;
            _instrumentService = instrumentService;
        }

        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;
            //Loaded += WebRequestTest;
            //Loaded += WebClientTest;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
        
        void WebClientTest(object sender, RoutedEventArgs e)
        {
            var webClient = new WebClient();
            webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
            webClient.DownloadStringAsync(new Uri("http://localhost/FinMkts.CFD.Rest/Instruments"));

        }
        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var s = new XmlSerializer(typeof(List<InstrumentDC>));
            

        }

        void WebRequestTest(object sender, RoutedEventArgs e)
        {
            var webRequest = WebRequest.Create("http://localhost/FinMkts.CFD.Rest/Instruments") as HttpWebRequest;
            if (webRequest != null) 
                webRequest.BeginGetResponse(response_callback, webRequest);

            webRequest = WebRequest.Create("http://localhost/FinMkts.CFD.Rest/CFD") as HttpWebRequest;
            if (webRequest != null)
                webRequest.BeginGetResponse(response_callback2, webRequest);

        }


        private void response_callback(IAsyncResult ar)
        {
            var webRequest = ar.AsyncState as HttpWebRequest;
            if (webRequest == null) return;

            WebResponse webResponse = webRequest.EndGetResponse(ar);

            var s = new XmlSerializer(typeof(List<InstrumentDC>));
            var instruments = (List<InstrumentDC>)s.Deserialize(webResponse.GetResponseStream());
        }

        private void response_callback2(IAsyncResult ar)
        {
            var webRequest = ar.AsyncState as HttpWebRequest;
            if (webRequest == null) return;

            WebResponse webResponse = webRequest.EndGetResponse(ar);

            var s = new XmlSerializer(typeof(List<MarketPlaceDC>));
            var marketplaces = (List<MarketPlaceDC>)s.Deserialize(webResponse.GetResponseStream());
        }

    }
}
